import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.NoSuchElementException;
import java.util.Scanner;

/**
  * Helper class for PipedAnalyzer.
  * Illustrates how a first level of analysis
  * would be done on data in a sequence of threads
  * connected by piped streams.
**/
class Analyze1Thread extends Thread {
  private PrintWriter fOut;
  private Scanner fScanner;

  public Analyze1Thread (Writer out, BufferedReader in) {
      fOut = new PrintWriter (out);
      fScanner  = new Scanner (in);
  }

  /** Read through the data until it runs out.
    * Do a calculation upon each value and
    * send the result to the output stream.
   **/
   public void run () {
    double d = 0.0;
    if (fOut != null && fScanner != null) {
      try {
        while (true) {
          d = fScanner.nextDouble ();
          // Send as string to the PrintWriter stream
          fOut.printf ("%12.5f %n",calculate (d));
          fOut.flush ();
        }
      }
      catch (NoSuchElementException nse)
      {} // data finished
      catch (Exception e) {
             System.err.println("Analyzer1Thread error = " + e);
      }
    }
    fOut.close ();
  } // run

  /** Do trivial calculation. **/
  double calculate (double d) {
    return d*d;
  }

} // Analyze1Thread